package com.class5.springboot.frame.web.result;

import java.io.PrintWriter;


import java.io.StringWriter;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;


import com.class5.springboot.frame.web.exception.RemoteInvokeException;
import com.class5.springboot.util.exception.AppException;
import lombok.extern.slf4j.Slf4j;




@SuppressWarnings("serial")
@Slf4j
public class ResultFailure extends ResultData {
    public ResultFailure() {
        setSuccess(false);
    }
    public ResultFailure(String errCode, String errMsg) {
        setSuccess(false);
        this.errcode = errCode;
        this.errmsg = errMsg;
    }
    public ResultFailure(Throwable a, boolean debugModel, HttpServletRequest req) {
        setSuccess(false);
        if(a != null) {
            if(a instanceof AppException){
                AppException ae = (AppException)a;
                if(ae.getCode() != null){
                    errcode = ae.getCode();
                }
                errmsg = ae.getMessage();
            }else{
                if(log.isDebugEnabled())
                    errmsg = a.getMessage();
                else
                    errmsg = "服务器未知异常";
            }
        }
        //输出调试信息
        if(debugModel) {
            Enumeration<String> names = req.getHeaderNames();
            JSONObject headers = new JSONObject();
            if(names != null) {
                while(names.hasMoreElements()) {
                    String name = names.nextElement();
                    headers.put(name, req.getHeader(name));
                }
            }
            names = req.getParameterNames();
            JSONObject params = new JSONObject();
            if(names != null) {
                while(names.hasMoreElements()) {
                    String name = names.nextElement();
                    params.put(name, req.getParameterValues(name));
                }
            }
            debug = new JSONObject();
            //请求头
            debug.put("header", headers);
            //请求参数
            debug.put("params", params);
            //请求体参数
            debug.put("bodyParams", req.getAttribute("_bodyobj"));
            //后台异常信息
            if(a != null) {
                String s = null;
                if(a instanceof RemoteInvokeException) {
                    s = ((RemoteInvokeException)a).getRemoteStack();
                    if(s != null)
                        debug.put("remoteexp", s.replace("\t", "    ").split("\n"));
                }else {
                    StringWriter sw = new StringWriter();
                    a.printStackTrace(new PrintWriter(sw));
                    s = sw.toString();
                    if(s != null)
                        debug.put("webexp", s.replace("\t", "    ").split("\n"));
                }

            }
        }
    }
    // 错误代码
    private String errcode = "500";
    // 错误消息内容
    private String errmsg = null;
    //调试信息
    private JSONObject debug;
    public String getErrcode() {
        return errcode;
    }
    public void setErrcode(String errcode) {
        this.errcode = errcode;
    }
    public String getErrmsg() {
        return errmsg;
    }
    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }
    @Override
    public String toString() {
        return "ResultFailure [errcode=" + errcode + ", errmsg=" + errmsg + "]";
    }
    public JSONObject getDebug() {
        return debug;
    }
    public void setDebug(JSONObject debug) {
        this.debug = debug;
    }
}
